package org.ch03.job.test06;

import java.lang.reflect.Method;
import java.lang.reflect.Parameter;

public class Summation {
    public void getSum(@Sum(max =100) int n) throws Exception {
        Max(n);
        int sum = 0;
        for (int i = 1; i <= n; i++) {
            sum+=i;

        }
        System.out.println("和："+sum);
    }
    public void Max(int n) throws Exception {
        Method method = this.getClass().getMethod("getSum", int.class);
        Parameter parameter = method.getParameters()[0];
        boolean flag = parameter.isAnnotationPresent(Sum.class);
        if (!flag){
            System.out.println("没有注解");
        }else {
            Sum sum = parameter.getAnnotation(Sum.class);
            if (n > sum.max()){
                throw new Exception("超过参数上限");
            }
        }
    }
}
